package com.lgy.springboot_example.service;

import com.alibaba.fastjson.JSONObject;
import com.lgy.springboot_example.entity.OrderInfo;
import com.lgy.springboot_example.mapper.OrderInfoMapper;
import com.lgy.springboot_example.utils.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderInfoService {
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    public DataResult<Object> getAll() {
        List<OrderInfo> list = orderInfoMapper.selectAll();
        List<OrderInfo> notPayOrderInfo = orderInfoMapper.getNotPayOrderInfo();
        JSONObject data = new JSONObject();
        data.put("tk.mybatis", list);
        data.put("mybatis", notPayOrderInfo);
        return new DataResult<>(data);
    }

    public DataResult<Object> addOrderInfo(OrderInfo orderInfo) {
        orderInfo.setCreateTime(new Date());
        Integer result = orderInfoMapper.insertUseGeneratedKeys(orderInfo);
        if (result < 0)
            return new DataResult<>(-1, "fail to insert");
        return new DataResult<>(orderInfo.getOrderId());
    }

    public DataResult<Object> updateOrderInfo(OrderInfo orderInfo) {
        Integer result = orderInfoMapper.updateByPrimaryKeySelective(orderInfo);
        if (result < 0)
            return new DataResult<>(-1, "fail to update");
        return new DataResult<>();
    }

    public DataResult<Object> delOrderInfoById(Integer orderId) {
        Integer result = orderInfoMapper.deleteByPrimaryKey(orderId);
        if (result < 0)
            return new DataResult<>(-1, "fail to delete");
        return new DataResult<>();
    }
}
